package com.luoc.base.redis;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;

/**
 * @Author: luoc
 * @Date: 2019/7/5 10:24
 */
@Configuration
@ConditionalOnProperty(prefix = "redis", name = "enabled", havingValue = "true")
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
    @Resource
    private RedisProperties redisProperties;

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(redisProperties.getMaxTotal());
        config.setMaxIdle(redisProperties.getMaxIdle());
        config.setMinIdle(redisProperties.getMinIdle());
        String password = null;
        if (StringUtils.isNotEmpty(redisProperties.getPassword())) {
            password = redisProperties.getPassword();
        }
        return new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), password);
    }

    @Bean
    @ConditionalOnMissingBean(RedisService.class)
    public RedisService redisService(JedisPool pool) {
        RedisService redisService = new RedisService();
        redisService.setJedisPool(pool);
        redisService.setPrefix(redisProperties.getPrefix());
        return redisService;
    }
}